<template>
  <div class="goodsBox">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 主体部分使用卡片 -->
    <el-card class="box-card">
      <div class="goodsMain" v-if="beforeAddGoods">
        <!-- 上边搜索框 -->
        <el-row type="flex">
          <el-col :span="12">
            <div class="grid-content bg-purple" style="text-align: left">
              <el-input
                clearable
                placeholder="请输入内容"
                class="input-with-select"
                v-model.trim="params.query"
                @clear="getSearchGoods"
                @keydown.native.enter="getSearchGoods"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="getSearchGoods"
                ></el-button>
              </el-input>
            </div>
          </el-col>

          <el-col :span="3">
            <div class="grid-content bg-purple">
              <el-row type="flex">
                <el-col :span="1"></el-col>
                <el-col :span="19">
                  <el-button type="primary" @click="addGoods"
                    >添加商品</el-button
                  >
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>

        <!-- 表格主体 -->
        <el-table :data="tableData" border style="width: 100%  margin-top:10px">
          <el-table-column type="index" width="50"> </el-table-column>
          <!-- prop为对应列内容的字段名 -->
          <el-table-column prop="goods_id" label="商品id" align="center">
          </el-table-column>
          <el-table-column prop="goods_name" label="商品名称" align="center">
          </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格(元)"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="goods_weight" label="商品重量" align="center">
          </el-table-column>
          <el-table-column prop="add_time" label="创建时间" align="center">
            <template slot-scope="scope">
              {{ scope.row.add_time | formDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="goods_state"
            label="操作"
            min-width="100"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                class="operate"
                @click="editGoods(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                class="operate"
                @click="deleteGoods(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pagenum"
          :page-sizes="[1, 2, 5, 10, 50]"
          :page-size="params.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalpage"
        >
          <!-- page-sizes为可选每页显示数据 -->
          <!-- page-size为每页实际展示数据 -->
          <!-- :total绑定了请求回来的数据总条数 -->
        </el-pagination>
      </div>
      <router-view v-else></router-view>
    </el-card>

    <!-- 修改商品信息对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editVisible">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item
          label="商品名称"
          prop="goods_name"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.goods_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="价格"
          prop="goods_price"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.goods_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="数量"
          prop="goods_number"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.goods_number"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="重量"
          prop="goods_weight"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="editForm.goods_weight"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getGoods, getDeleteGoods, getEditGoods } from "../../api/index";
export default {
  data() {
    return {
      beforeAddGoods: true,
      tableData: [],
      totalpage: 0, //总页数
      // 分页数据
      params: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 修改商品
      editVisible: false,
      editForm: {
        goods_name: "",
        goods_price: "",
        goods_number: "",
        goods_weight: ""
      },
      formLabelWidth: "200px",
      rules: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],

        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async getGoodsList() {
      let {
        data: { total, pagenum, goods },
        meta: { msg, status },
      } = await getGoods(this.params);
      if(status != 200) return this.$message.error(msg)
      // console.log(pagenum);
      this.tableData = goods;
      this.params.pagenum = pagenum*1;
      this.totalpage = total;
    },
    // 分页功能
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      // 每页显示数据的改变即为向后台发送数据请求,得到相应的页数和数据量然后渲染
      this.params.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.getGoodsList();
    },
    // 搜索功能
    getSearchGoods() {
      this.getGoodsList();
    },
    addGoods() {
      this.$router.push("/goods/add");
      this.beforeAddGoods = false;
    },
    // 删除商品
    async deleteGoods(data) {
      let res = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch(() => {});

      if (res != "confirm") return this.$message("取消操作");
      let {
        meta: { msg, status },
      } = await getDeleteGoods(data.goods_id);
      if (status == 200) {
        this.$message.success(msg);
        this.getGoodsList();
      } else {
        this.$message(msg);
      }
    },
    // 编辑商品
    editGoods(data) {
      this.editVisible = true;
      this.editForm = data;
      this.editForm.goods_cat = '111'
    },
    async editGoodsData() {
      let success = await this.$refs["editForm"].validate().catch(() => {});
      if (!success) return false;
      // 发送修改信息请求
      let {
        meta: { msg, status },
      } = await getEditGoods(this.editForm);
      if (status == 200) {
        this.$message.success(msg);
        this.editVisible = false;
        this.getGoodsList();
      } else {
        this.$message.error(msg);
      }
    }
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style scoped>
.box-card {
  width: 100%;
  max-height: 820px;
  overflow: auto;
}

.goodsBox {
  padding-left: 20px;
}

.el-breadcrumb {
  font-size: 12px;
  background-color: inherit;
  margin-bottom: 0;
}

.el-table {
  margin-top: 20px;
  max-height: 650px;
  overflow: auto;
}

.el-pagination {
  margin-top: 15px;
  text-align: center;
}
</style>
